<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img class="avatar" src="../assets/logo.png" />
      </div>
      <el-form
        ref="loginFormRef"
        :rules="loginRules"
        :model="loginForm"
        label-width="0px"
        class="login_form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="loginDo">登录</el-button>
          <el-button type="info" @click="resetValue">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    }
  },
  methods: {
    // 重置表单
    resetValue: function () {
      this.$refs.loginFormRef.resetFields()
    },
    loginDo: function () {
      this.$refs.loginFormRef.validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.$http.post("login", this.loginForm)
          // eslint-disable-next-line eqeqeq
          if (res.meta.status != 200) return this.$message.error("登陆失败")
          this.$message.success("登陆成功")
          // eslint-disable-next-line eqeqeq
          //  登录成功之后将token 存入sessionStorage中 每次访问都携带
          // 成功之后 路由导航到首页
          window.sessionStorage.setItem("token", res.data.token)
          this.$router.push("/home")
        }
      })
    },
  },
}
</script>
<style scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box {
  height: 120px;
  width: 120px;
  border: 1px solid #eee;
  position: absolute;
  box-shadow: 0 0 10px #ddd;
  left: 50%;
  border-radius: 50%;
  transform: translate(-50%, -50%);
}
.avatar {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: #fff;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
